<?php
class User extends CI_Controller {

	private $aPost;
	private $aUser;

	public function __construct() {

		parent::__construct();
		if (! $this->usersession->userLoggedIn()) {
			$aResponse = array("error" => TRUE, "description" => "Sesiunea a expirat", "type" => "nosession");
			die(json_encode($aResponse));
		}
		$this->load->model('model_user');
		$this->aUser = $this->usersession->currentUser();
		copyPost($this->aPost);
	}

	/**
	 * Functie care aduce datele in grid
	 */
	function getData() {
		echo json_encode($this->model_user->getData($this->aPost));
	}

	/**
	 * Functie care aduce datele despre unei inregistrari
	 * @return string json
	 */
	function getUser() {
		$aResult = $this->model_user->getUser($this->aPost);
		echo json_encode(array("error" => FALSE, "data" => $aResult));
	}

	/**
	 * Functie de editare / inserare element
	 * @return string json
	 */
	function operation() {

		$bResponse = $this->model_user->operation($this->aPost);
		if ($bResponse === TRUE) {
			$aResponse = array("error" => FALSE, "success" => TRUE, "description" => "Datele au fost salvate");
		} else {
			$aResponse = array("error" => TRUE, "description" => $bResponse, "type" => "Validare");
		}
		echo  json_encode($aResponse);
	}


	/**
	 * Functie care salveaza semnatura
	 */
	function saveSign() {
		$aUser = $this->usersession->currentUser();
		$this->db->set("sign", $this->aPost["sign"]);
		$this->db->where("user_id", $aUser["user_id"]);
		$this->db->update("user");
		$aResponse = array("error" => FALSE, "description" => "Semnatura a fost schimbata");
		echo json_encode($aResponse);
	}

	/**
	 * Functie aduce semnatura
	 */
	function getSign() {
		$aUser = $this->usersession->currentUser();
		$this->db->set("sign", $this->aPost["sign"]);
		$this->db->where("user_id", $aUser["user_id"]);
		$sSign = $this->db->get("user")->row();
		$aResponse = array("error" => FALSE, "data" => $sSign);
		echo json_encode($aResponse);
	}


	/** Trimitere semnatura pe email
	 * @return
	 */
	function sendSign() {

		$this->load->plugin('phpmailer');
		$path_full = $this->config->item('upload_logo');

		$this->db->where("user_id", $this->aUser["user_id"]);
		$aUser = $this->db->get("user")->row_array();

		$this->db->where("code", "logo");
		$sLogo = $this->db->get("settings")->row()->value;

		$mail = new phpmailer(TRUE);

		$mail->IsHTML(TRUE);
		$mail->IsSMTP();
		$mail->SMTPAuth = TRUE;

		try {

			$mail->CharSet = "utf-8";
			$mail->Host = "mail.morethanpub.ro";
			$mail->Password = "officeoffice";
			$mail->Username = "office@morethanpub.ro";
			$mail->Port = 26;


			$mail->AddReplyTo($this->aUser["email"], $this->aUser["name"]);
			$mail->SetFrom("office@morethanpub.ro", "MTP CRM");
			$mail->AddAddress($this->aUser["email"]);

			$filename = "././resources/logo/".$sLogo;
			$body = $aUser["sign"];


			if (file_exists($filename)) {

				$pattern = '/_LOGO/';
				preg_match_all($pattern, $body, $matches);

				if (count($matches[0]) > 0) {
					//scot calea relativa
					$body = str_replace($path_full, "", $body);
					//$sReplace="cid:".$attach["name"];
					$aReplace = explode(".", $sLogo);
					array_pop($aReplace);
					$base = implode(".", $aReplace);
					$sReplace2 = "cid:".$base;

					$body = str_replace("_LOGO", "<img src='$sReplace2'  />", $body);
					$mail->AddEmbeddedImage($filename, $base, $sLogo);
				}
			}

			$mail->Subject = "MTP - Semnatura Test";
			$mail->Body = "<body>".$body."</body>";

			if (! $mail->Send()) {
				$aResponse = array("error" => TRUE, "description" => "Emailul NU a fost trimis: ".$mail->ErrorInfo);
			} else {
				$aResponse = array("error" => FALSE, "description" => "Emailul a fost trimis");
			}
		} catch (phpmailerException $e) {
			$aResponse = array("error" => TRUE, "description" => "Emailul NU a fost trimis: ".$e->errorMessage(), "type" => "email");
		}
		catch (Exception $e) {
			$aResponse = array("error" => TRUE, "description" => "Emailul NU a fost trimis: ".$e->getMessage(), "type" => "email");
		}
		echo json_encode($aResponse);
	}

	/**
	 * Functie care schimba parola unui user
	 * @return string json
	 */
	function addPass() {
		$this->model_user->addPass($this->aPost);
		$aResponse = array("error" => FALSE, "description" => "Parola a fost schimbata");
		echo json_encode($aResponse);
	}

}
